package com.qianfeng.controller;

import com.qianfeng.dto.Message;
import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Department;
import com.qianfeng.enums.CommonEnum;
import com.qianfeng.exceptions.CommonException;
import com.qianfeng.service.DepartmentService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 添加
     *
     * @param department
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/add_department", method = RequestMethod.POST)
    public Result addDepartment(Department department) {
        try {
//            int i = 1 / 0;
            departmentService.addDepartment(department);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.ADD_DEPT_ERROR);
        }
    }

    /**
     * 删除
     *
     * @param deptId
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/delete_department")
    public Result deleteDept(Integer deptId) {
        try {
//            int i = 1 / 0;
            departmentService.deleteDepartment(deptId);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.DELETE_DEPT_ERROR);
        }
    }

    /**
     * 更新
     *
     * @param department
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/update_department", method = RequestMethod.POST)
    public Result updateDepartment(Department department) {
        try {
//            int i = 1 / 0;
            departmentService.updateDepartment(department);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.UPDATE_DEPT_ERROR);
        }
    }

    /**
     * 通过id查
     *
     * @param deptId
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getDepartmentByDeptId")
    public Result getDeptByDeptId(Integer deptId) {

        Department departmentByDeptId = departmentService.getDepartmentByDeptId(deptId);
        Result result = new Result();
        Message message = new Message();
        result.setMessage(message);
        result.setObj(departmentByDeptId);
        return result;

    }


    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getDepartmentPageJson")
    public Result getDeptPage(Integer pageNo, Integer pageSize) {
        Result result = new Result();
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 2;
        }
        Page deptPage = departmentService.getDeptPage(pageNo, pageSize);
        result.setObj(deptPage);
        Message message = new Message();
        message.setCode(200);
        message.setMsg("成功");
        result.setMessage(message);
        return result;
    }
}
